#include "OiPCH.hpp"
#include "View/OiPixmap.hpp"

namespace Oi
{
    namespace PLScannerCore
    {

        class EachRecordWidgetImpl : public Impl<EachRecordWidget>
        {
        public:
            void init(WorkResult::ResultData data);

        public:
            WorkResult::ResultData _data;
        };

        void EachRecordWidgetImpl::init(WorkResult::ResultData data)
        {
            OI_Q(EachRecordWidget);

            _data = data;
            
            QHBoxLayout* _toolsLayout = new QHBoxLayout(q);

            int index = 0;

            bool result = false;

            for(auto iter = data.begin(); iter != data.end(); ++iter)
            {
                QLabel *label = new QLabel();
                QVBoxLayout* VLayout = new QVBoxLayout(label);

                QLabel *labelTitle = new QLabel(iter.key());
                labelTitle->setAlignment(Qt::AlignCenter);
                labelTitle->setObjectName("_ToolName_" + QString::number(index));
                labelTitle->setStyleSheet("QLabel#_ToolName_" + QString::number(index) + "{border:1px solid rgb(0, 0, 0);}");
                VLayout->addWidget(labelTitle);

                QLabel *labelResult = new QLabel();
                QHBoxLayout* HLayout = new QHBoxLayout(labelResult);

                int index1 = 0;
                for(QVariantMap::iterator iter1 = iter.value().begin(); iter1 != iter.value().end(); ++iter1)
                {
                    if(iter1.key() != "result" && iter1.key() != "Value")
                    {
                        QLabel *labelOneResult = new QLabel();
                        QVBoxLayout* VLayoutOneResult = new QVBoxLayout(labelOneResult);

                        QLabel *labelOneType = new QLabel(iter1.key());
                        labelOneType->setAlignment(Qt::AlignCenter);


                        QLabel *labelOneTypeR = new QLabel(iter1.value().toString());
                        labelOneTypeR->setAlignment(Qt::AlignCenter);

                        labelOneType->adjustSize();
                        labelOneTypeR->adjustSize();
                        VLayoutOneResult->addWidget(labelOneType);
                        VLayoutOneResult->addWidget(labelOneTypeR);
                        labelOneResult->adjustSize();
                        HLayout->addWidget(labelOneResult);

                    }
                    else
                    {
                        result = iter1.value().toBool();
                    }
                }

                labelResult->setObjectName("_ToolResult_" + QString::number(index));
                labelResult->setStyleSheet("QLabel#_ToolResult_" + QString::number(index) + "{border:1px solid rgb(0, 0, 0);}");
                labelResult->adjustSize();
                VLayout->addWidget(labelResult);
                label->adjustSize();
                label->setObjectName("Toollabel_" + QString::number(index));
                if(result)
                {
                    label->setStyleSheet("QLabel#Toollabel_" + QString::number(index) + "{border:2px solid rgb(0, 255, 0);background-color:rgba(0,255,0,50);}");
                }
                else
                {
                    label->setStyleSheet("QLabel#Toollabel_" + QString::number(index) + "{border:2px solid rgb(255, 0, 0);background-color:rgba(255,0,0,50);}");
                }

                _toolsLayout->addWidget(label);
                index++;
            }
        }

        EachRecordWidget::EachRecordWidget(WorkResult::ResultData data,  QWidget * parent /*= nullptr*/) : QWidget(parent)
        {
            OI_I(EachRecordWidget)->init(data);
        }

        EachRecordWidget::~EachRecordWidget()
        {
            OI_E(EachRecordWidget);
        }

        WorkResult::ResultData EachRecordWidget::data() const
        {
            return OI_IMPL()->_data;
        }

    }
}